package com.inspur.cmis.base.cust.action;

import com.inspur.cmis.base.cust.model.CustBean;
import com.inspur.cmis.base.cust.service.CustService;
import com.inspur.common.model.Pagination;
import com.inspur.common.util.ConstUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/cust")
public class CustController {

    private static final Logger log = LoggerFactory.getLogger(CustController.class);

    @Autowired
    CustService custService;
    @Resource
    HttpServletRequest request;
    @RequestMapping("responseJson")
    public @ResponseBody
    CustBean responseJson(CustBean itemsCustom){

        return itemsCustom;
    }

    @RequestMapping("/getCustListByPage")
    public String getCustListByPage(int pageNo, int pageSize,  CustBean custBean, Model model) {

        Pagination<CustBean> page = new Pagination<CustBean>(pageNo, pageSize);
        log.debug("CustController getCustListByPage = "+page);
        page.setQueryObject(custBean);
        custService.getCustListByPage(page);
        request.setAttribute("custlist", page);
        return "base/cust/cust_list";
    }
    @RequestMapping("/addCust")
    public String addCust(CustBean custBean){
        String url="base/cust/clientInfoAdd";
        if(request.getMethod().equals("POST")){
            custService.addCust(custBean);
            url="redirect:/cust/getCustListByPage?pageNo=1&pageSize=10";
        }

        return url;
    }
    @RequestMapping("/addCustSave")
    public String addCustSave(@Validated @ModelAttribute("custBean")CustBean custBean, BindingResult bindingResult){
        String url="base/cust/clientInfoAdd";
        if(!bindingResult.hasErrors()){
            custService.addCust(custBean);
            url="redirect:/cust/getCustListByPage?pageNo=1&pageSize=10";
        }

        return url;
    }
    @RequestMapping("/delCust")
    public String delCust(CustBean custBean){
        String url="redirect:/cust/getCustListByPage?pageNo=1&pageSize=10";;

        custService.delCust(custBean);
        url="redirect:/cust/getCustListByPage?pageNo=1&pageSize=10";


        return url;
    }

    @RequestMapping("/updateCustView")
    public String updateCustView(CustBean custBean){
        String url="base/cust/clientInfoUpdate";

        Pagination<CustBean> page = new Pagination<CustBean>(1, 10);
        page.setQueryObject(custBean);
        custService.getCustListByPage(page);
        request.setAttribute("cust",page.getList().get(0));

        return url;
    }
    @RequestMapping("/updateCust")
    public String updateCust(CustBean custBean){
        String url="redirect:/cust/getCustListByPage?pageNo=1&pageSize=10";
        custService.updateCust(custBean);

        return url;
    }
}
